package com.heima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.domain.Child;
import com.heima.domain.Department;
import com.heima.mapper.DepartmentMapper;
import com.heima.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.vo.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 科室 服务实现类
 * </p>
 *
 * @author 黑马程序员
 * @since 2023-01-29
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<DepartmentVo> getAll(Integer page, Integer pageSize) {
        //MYbaits plus 的分页格式
        IPage<Department> pageInfo = new Page<>();
        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        //条件是相同医院下可用的科室
        wrapper.eq(Department::getHospitalId, 1);
        wrapper.eq(Department::getState, true);
        IPage<Department> departmentIPage = departmentMapper.selectPage(pageInfo, wrapper);
        if (departmentIPage == null) {
            return new ArrayList<>();
        }
        //将查到的科室条件集合转换成list集合
        List<Department> records = departmentIPage.getRecords();
        //获取一级科室的id集合
        List<Integer> collect = records.stream().filter(Objects::nonNull).map(Department::getId).collect(Collectors.toList());
        //根据一级科室查出二级科室
        LambdaQueryWrapper<Department> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(Department::getParent, collect);
        wrapper1.eq(Department::getState, true);
        List<Department> selectList = departmentMapper.selectList(wrapper1);
        List<DepartmentVo> vos = new ArrayList<>();
        //如果没查到就返回一级科室
        if (!CollectionUtil.isEmpty(selectList)) {
            //遍历一级科室集合
            for (Department record : records) {
                if (record == null) {
                    continue;
                }
                DepartmentVo departmentVo = new DepartmentVo();
                //一级科室id
                Integer id = record.getId();
                //用来储存二级科室信息
                List<Child> children = new ArrayList<>();
                for (Department department : selectList) {
                    if (department == null) {
                        continue;
                    }
                    Integer parent = department.getParent();
                    if (id.equals(parent)) {
                        //匹配成功，就把信息存贮到child中
                        Child child = new Child();
                        BeanUtils.copyProperties(department, child);
                        children.add(child);
                    }
                }
                //遍历完就收集完成了
                departmentVo.setChild(children);
                departmentVo.setId(id);
                departmentVo.setName(record.getName());
                vos.add(departmentVo);
            }
        }
        return vos;
    }
}
